/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package l1j.server.server.clientpackets;


import l1j.server.Config;
import l1j.server.server.ClientThread;
import l1j.server.server.GetNowTime;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.gametime.L1GameRestart;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.serverpackets.S_WhoAmount;
import l1j.server.server.serverpackets.S_WhoCharinfo;

// Referenced classes of package l1j.server.server.clientpackets:
// ClientBasePacket

/**
 * 處理收到由客戶端傳來查詢線上人數的封包
 */
public class C_Who extends ClientBasePacket {

	private static final String C_WHO = "[C] C_Who";

	public C_Who(byte[] decrypt, ClientThread client) {
		super(decrypt);
		String s = readS();
		L1PcInstance find = L1World.getInstance().getPlayer(s);
		L1PcInstance pc = client.getActiveChar();

		if (find != null) {
			S_WhoCharinfo s_whocharinfo = new S_WhoCharinfo(find);
			pc.sendPackets(s_whocharinfo);
		}
		else {
			if (Config.ALT_WHO_COMMAND) {
				String amount = String.valueOf(L1World.getInstance().getAllPlayers().size());
				S_WhoAmount s_whoamount = new S_WhoAmount(amount);
				pc.sendPackets(s_whoamount);
				pc.sendPackets(new S_SystemMessage("【經驗】"+Config.RATE_XP+"倍【正義】"+Config.RATE_LA+"倍【金錢】"+Config.RATE_DROP_ADENA+"倍"));
                pc.sendPackets(new S_SystemMessage("【好友】"+Config.RATE_KARMA+"倍【角色負重】"+Config.RATE_WEIGHT_LIMIT+"倍【掉寶】"+Config.RATE_DROP_ITEMS+"倍"));
                pc.sendPackets(new S_SystemMessage("【武器】"+Config.ENCHANT_CHANCE_WEAPON+"倍【防具】"+Config.ENCHANT_CHANCE_ARMOR+"倍【寵物負重】"+Config.RATE_WEIGHT_LIMIT_PET+"倍"));
                pc.sendPackets(new S_SystemMessage("【寵物經驗】"+Config.RATE_SHOP_SELLING_PRICE+"倍【收購】"+Config.RATE_SHOP_PURCHASING_PRICE+"倍"));
               
				//add今天日期
				int Mon = GetNowTime.GetNowMonth();//add月份錯誤補正
				pc.sendPackets( new S_SystemMessage("☆"+"今天是 " + GetNowTime.GetNowYear() + " 年 "
						+ (Mon+1)+ " 月 " + GetNowTime.GetNowDay() + " 日☆"));
				
				//add目前時間
				pc.sendPackets( new S_SystemMessage("☆"+"現在時刻" + GetNowTime.GetNowHour() + " 時 "
						+ GetNowTime.GetNowMinute() + " 分 " + GetNowTime.GetNowSecond() + " 秒☆"));

//				add 玩家查詢系統倒數時間
				int second = L1GameRestart.getWillRestartTime();
				pc.sendPackets(new S_SystemMessage("☆距離伺服器重開時間還有 " + ( second / 60 ) / 60 + " 小時 " + ( second / 60 ) % 60+ " 分 " + second % 60 + " 秒☆"));
//				add end
			
// add end
		          int i=1;
	                for (L1PcInstance pc1 : L1World.getInstance().getAllPlayers()) {
	                	String lvmetname = "";
	         	       if (pc.getAccessLevel()>=100){
	         	    	   lvmetname = "\\f2【遊戲管理員】";}
	         	       else{
	         	    	   switch (pc.getlevelmet()){	       
	         	        	  case 0: lvmetname = "";break;
	         	        	  case 1: lvmetname = "\\f=【1轉】";break;   
	         	        	  case 2: lvmetname = "\\f=【2轉】";break; 
	         	        	  case 3: lvmetname = "\\f=【3轉】";break; 
	                                   case 4: lvmetname = "\\f=【4轉】";break;  
	         	        	  case 5: lvmetname = "\\f=【5轉】";break; 
	         	        	  case 6: lvmetname = "\\f=【6轉】";break; 
	                           case 7: lvmetname = "\\f=【7轉】";break; 
	                           case 8: lvmetname = "\\f=【8轉】";break; 
	                           case 9: lvmetname = "\\f=【9轉】";break; 
	         } 
	         	       }
						if (pc.isGm() == true) {
		                        pc.sendPackets(new S_SystemMessage(i+"【玩家】【"+pc1.getName()+"】"+lvmetname+"【"+pc1.getClanname()+"】【等級】"+pc1.getLevel()+"】"));
		                 	} else {
	                 		pc.sendPackets(new S_SystemMessage(i+"【玩家】【"+pc1.getName()+"】"+lvmetname+"【"+pc1.getClanname()+"】"));
	                   	}

	                    i++;
	                }
			}
			// TODO: ChrisLiu: SystemMessage 109
			// 顯示消息如果目標是不存在？正方修知道，謝謝你。
		}
	}

	@Override
	public String getType() {
		return C_WHO;
	}
}
